﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yc.Dal;

namespace yc.Site.Controllers
{
	public class CompanyController : Controller
	{
		public ActionResult Index(FormCollection c)
		{
			var Contact = c.Get("Contact");
			var Telephone = c.Get("Telephone");
			var db = new DBContext();

			IQueryable<SysCompany> query = db.SysCompany;

			if (!string.IsNullOrEmpty(Contact)) query = query.Where(p => p.SysCompanyContact.Any(q => q.Contact.Contains(Contact)));
			if (!string.IsNullOrEmpty(Telephone)) query = query.Where(p => p.SysCompanyContact.Any(q => q.Telephone.Contains(Telephone)));

			var pager = query.ToPager("CreateDate");
			return View(pager);
		}

		public ActionResult Edit(int Id = 0)
		{
			var db = new DBContext();
			var item = new SysCompany();
			if (Id != 0) item = db.SysCompany.Find(Id);

			return View(item);
		}

		[HttpPost, ActionName("Edit")]
		public ActionResult PostEdit(int Id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysCompany { CreateDate = DateTime.Now };
			if (Id != 0) item = db.SysCompany.Find(Id);
			if (TryUpdateModel(item))
			{
				if (Id == 0) db.SysCompany.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
			}
			return View(item);
		}

		public ActionResult Print(int Id)
		{
			var db = new DBContext();
			var item = db.SysCompany.Find(Id);

			return View(item);
		}

		#region Contact
		public ActionResult EditContact(int pid = 0, int wid = 0)
		{
			var item = new SysCompanyContact { CID = pid };
			var db = new DBContext();
			if (wid != 0) item = db.SysCompanyContact.Find(wid);
			return View(item);
		}

		[HttpPost, ActionName("EditContact")]
		public ActionResult PostEditContact(int id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysCompanyContact();
			if (id != 0) item = db.SysCompanyContact.Find(id);
			if (TryUpdateModel(item))
			{
				if (id == 0) db.SysCompanyContact.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
				ViewBag.Saved = 1;
			}
			return View(item);
		}

		[HttpPost]
		public ActionResult DelContact(int wid = 0)
		{
			if (wid == 0) return Json(new { res = false, msg = "数据非法" });
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = db.SysCompanyContact.Find(wid);
			if (item == null) return Json(new { res = false, msg = "数据非法" });
			db.SysCompanyContact.Remove(item);
			db.SaveChanges();
			return Json(new { res = true, msg = "删除成功" });
		}
		#endregion

		#region Employee
		public ActionResult EditEmployee(int pid = 0, int wid = 0)
		{
			var item = new SysCompanyEmployee { CID = pid };
			var db = new DBContext();
			if (wid != 0) item = db.SysCompanyEmployee.Find(wid);

			if (string.IsNullOrEmpty(item.Workplace))
			{
				var company = db.SysCompany.Find(item.CID);
				if (company != null) item.Workplace = company.Attribution;
			}

			ViewBag.Gender = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", item.Gender));
			ViewBag.Education = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", item.Education));
			ViewBag.SaleryType = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("SaleryType")).OrderBy(p => p.Sort), "Worth", "Caption", item.SaleryType));
			ViewBag.TrailMonth = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("TrailMonth")).OrderBy(p => p.Sort), "Worth", "Caption", item.TrailMonth));
			ViewBag.HouseholdRequirements = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("HouseholdRequirements")).OrderBy(p => p.Sort), "Worth", "Caption", item.HouseholdRequirements));
			
			return View(item);
		}

		[HttpPost, ActionName("EditEmployee")]
		public ActionResult PostEditEmployee(int id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysCompanyEmployee();
			if (id != 0) item = db.SysCompanyEmployee.Find(id);
			if (TryUpdateModel(item))
			{
				if (id == 0) db.SysCompanyEmployee.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
				ViewBag.Saved = 1;
			}

			ViewBag.Gender = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", item.Gender));
			ViewBag.Education = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", item.Education));
			ViewBag.SaleryType = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("SaleryType")).OrderBy(p => p.Sort), "Worth", "Caption", item.SaleryType));
			ViewBag.TrailMonth = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("TrailMonth")).OrderBy(p => p.Sort), "Worth", "Caption", item.TrailMonth));
			ViewBag.HouseholdRequirements = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("HouseholdRequirements")).OrderBy(p => p.Sort), "Worth", "Caption", item.HouseholdRequirements));

			return View(item);
		}

		[HttpPost]
		public ActionResult DelEmployee(int wid = 0)
		{
			if (wid == 0) return Json(new { res = false, msg = "数据非法" });
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = db.SysCompanyEmployee.Find(wid);
			if (item == null) return Json(new { res = false, msg = "数据非法" });
			db.SysCompanyEmployee.Remove(item);
			db.SaveChanges();
			return Json(new { res = true, msg = "删除成功" });
		}

		public ActionResult PrintEmployee(int id = 0)
		{
			var db = new DBContext();
			var item = db.SysCompanyEmployee.Find(id);
			return View(item);
		}
		#endregion
	}
}
